/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simon;

import java.util.ArrayList;


/**
 *
 * @author isen
 */
public class QueryTranslator {
    
    //méthode de traduction de la requête en arbre (pour le moment elle retourne rien pour les besoins des tests
    public static void translate(String q){
        
        ArrayList<String> requete = cut(q.toLowerCase(), "select");
        requete.remove(0);
        
        
        requete = cut(requete.get(0).toString(), "from");
        String select = requete.get(0).toString();
        select = select.trim();
        requete.remove(0);
        
        requete = cut(requete.get(0).toString(), "where");
        String from = requete.get(0).toString();
        from = from.trim();
        String where = requete.get(1).toString();
        where = where.trim();
        
        System.out.println(
                "le Select : " + select
                 + "\nle From : " + from
                 + "\nle Where : " + where
                );
        ArrayList<String> attributes = cut(select, ",");
        ArrayList<String> tables = cut(from, ",");
        ArrayList<String> conditions = cut(where, ",");
        
        System.out.println(attributes);
        System.out.println(tables);
        System.out.println(conditions);
       
        
        for (int i = 0; i < attributes.size(); i++) {
            System.out.println("Attribut " + (i+1) +": " +attributes.get(i));
        }
        for (int i = 0; i < tables.size(); i++) {
            System.out.println("table " + (i+1) +": " +tables.get(i));
        }
        for (int i = 0; i < conditions.size(); i++) {
            System.out.println("condition" + (i+1) +": " +conditions.get(i));
        }
        
    }
    
    // Fonction de découpage de string en fonction d'un séparateur
    public static ArrayList<String> cut(String ligneEntree, String separateur) {
        if (ligneEntree == null) {
            return null;
        }
        int index = 0;
        ArrayList lig = new ArrayList();
        String temporaire = ligneEntree;
        if (temporaire != null) {
            index = temporaire.indexOf(separateur);
            while (index >= 0) {
                lig.add(temporaire.substring(0, index));
                temporaire = temporaire.substring(index + separateur.length(), temporaire.length());
                index = temporaire.indexOf(separateur);
            }
            // Le dernier élément
            lig.add(temporaire);
        }
        return lig;
    }
    
    
}
